﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:59
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  AttachmentRepository : Repository<Attachment, Attachment, TbAttachment>, IAttachmentRepository<Attachment>
    {
        public override Attachment CreateEntity()
        {
            return new Attachment { };
        }
        public override IQueryable<Attachment> Get()
        {
            return MapQueryable(Entities.TbAttachment);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbAttachment).Count();
        }
        public override IQueryable<Attachment> Get(Expression<Func<Attachment, bool>> expression)
        {
            return MapQueryable(Entities.TbAttachment).Where(expression);
        }
        public override IQueryable<Attachment> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAttachment), sort, order);
        }
        public override IQueryable<Attachment> GetSort(Expression<Func<Attachment, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAttachment).Where(expression), sort, order);
        }
        public override void Add(Attachment t)
        {
            Entities.AddToTbAttachment(ReverseMapModel(t));
        }
        public override void Update(Attachment t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Attachment t)
        {
            if (Entities.TbAttachment.Any(o => o.CoAttachmentId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbAttachment(ReverseMapModel(t));
            }
        }
        public override void Delete(Attachment t)
        {
            Entities.TbAttachment.Where(o => o.CoAttachmentId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Attachment, bool>> expression)
        {
            MapQueryable(Entities.TbAttachment).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Attachment> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Attachment>();
        }
      
        public override sealed IQueryable<Attachment> MapQueryable(IQueryable<TbAttachment> queryable)
        {
            return queryable.Select(o => new Attachment 
            {
         
	           Id=o.CoAttachmentId,
	           Url = o.CoUrl 
,
	           IsDelete = o.CoIsDelete??false 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           Title = o.CoTitle 
,
	           Descript = o.CoDescript 
,
	           Sort = o.CoSort??0 
,
	           BelongsType = o.CoBelongsType??0 
,
	           BelongsId = o.CoBelongsId 
,
	           Culture = o.CoCulture 
,
	           Type = o.CoType??0 
            
            });
        }
      
        public override sealed IQueryable<TbAttachment> ReverseMapQueryable(IQueryable<Attachment> queryable)
        {
            return queryable.Select(o => new TbAttachment 
            { 
        
	           CoAttachmentId=o.Id,
	           CoUrl = o.Url
,
	           CoIsDelete = o.IsDelete
,
	           CoCreateTime = o.CreateTime
,
	           CoTitle = o.Title
,
	           CoDescript = o.Descript
,
	           CoSort = o.Sort
,
	           CoBelongsType = o.BelongsType
,
	           CoBelongsId = o.BelongsId
,
	           CoCulture = o.Culture
,
	           CoType = o.Type
  
            
            });
        }
      
        public override sealed Attachment MapModel(TbAttachment model)
        {
            return new Attachment {
	           Id=model.CoAttachmentId,
	           Url = model.CoUrl
,
	           IsDelete = model.CoIsDelete??false
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           Title = model.CoTitle
,
	           Descript = model.CoDescript
,
	           Sort = model.CoSort??0
,
	           BelongsType = model.CoBelongsType??0
,
	           BelongsId = model.CoBelongsId
,
	           Culture = model.CoCulture
,
	           Type = model.CoType??0
            };
        }
        
        public override sealed TbAttachment ReverseMapModel(Attachment model)
        {
            return new TbAttachment { 
	           CoAttachmentId=model.Id,
	           CoUrl = model.Url
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoTitle = model.Title
	        
,
	           CoDescript = model.Descript
	        
,
	           CoSort = model.Sort
	        
,
	           CoBelongsType = model.BelongsType
	        
,
	           CoBelongsId = model.BelongsId
	        
,
	           CoCulture = model.Culture
	        
,
	           CoType = model.Type
	        
            
            
            };
        }


    }
}



